package com.ark.controller;

import com.ark.common.constant.Constants;
import com.ark.common.domain.entity.sys.SysMenu;
import com.ark.common.domain.model.LoginBody;
import com.ark.common.response.Result;
import com.ark.common.utils.SecurityUtils;
import com.ark.security.service.LoginService;
import com.ark.service.service.SysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
public class LoginController {

    @Resource
    private LoginService loginService;

    @Resource
    private SysMenuService menuService;
    /**
     * 登录账号
     * @param loginBody 参数
     * @param request 请求对象
     */
    @PostMapping("/login")
    public Result login(@Valid @RequestBody LoginBody loginBody, HttpServletRequest request){
        Result result= Result.success();
        String token = loginService.login(loginBody,request);
        result.put(Constants.TOKEN, token);
        return result;
    }

    /**
     * 获取路由
     * @return
     */
    @GetMapping("/getRouters")
    public Result getRouters(){
        List<SysMenu> list = menuService.selectMenusByUserId(SecurityUtils.getUserId());

        return Result.success(menuService.buildRouters(list));
    }

}
